// pages/home/home.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hidden:"",
    search:"",
    show: false,
    total:"",
    shoptotal:"",
    footclass:[
      {name:"主食",
      url:"https://img.yzcdn.cn/vant/cat.jpeg"},
      {name: "水果",
      url:"https://img.yzcdn.cn/vant/cat.jpeg"},
      {name: "饮料",
      url:"https://img.yzcdn.cn/vant/cat.jpeg"},
      {name:"宵夜",
      url:"https://img.yzcdn.cn/vant/cat.jpeg"}
      ],
shop:[{
  shopname:"店铺名",
  sale:"176",
 shopid:"123"
},{
  shopname:"店铺名",
  sale:"176",
 shopid:"12344"
},{
  shopname:"店铺名",
  sale:"176",
 shopid:"12356"
},{
  shopname:"店铺名",
  sale:"176",
 shopid:"123"
},{
  shopname:"店铺名",
  sale:"176",
 shopid:"123"
},{
  shopname:"店铺名",
  sale:"176",
 shopid:"123"
},
],
newshop:[{
  shopname:"店铺名",
  sale:"1777",
 shopid:"123"
},{
  shopname:"店铺名",
  sale:"1777",
 shopid:"123"
},{
  shopname:"店铺名",
  sale:"17",
 shopid:"123"
},{
  shopname:"店铺名",
  sale:"16",
 shopid:"123"
},{
  shopname:"店铺名",
  sale:"176",
 shopid:"123"
},{
  shopname:"店铺名",
  sale:"176",
 shopid:"123"
},],
shoping:[],
food:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    let food = that.data.food
   let newtotal
for (let i = 0; i < food.length; i++) {
 
  newtotal = parseInt(food[i].total+that.data.total)
  // console.log(that.data.total)
  this.setData({
    total : newtotal 
  })
}
let newtotalprice = that.data.total*100
// console.log(newtotalprice)
this.setData({
  total:newtotalprice
})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  round:function(e){
    this.setData({
      search: e.detail,
    });
    console.log(this.data.search)
    // Toast('搜索' + this.data.value);
   
  },
  onClick:function(){
    let that = this
    wx.request({
      url: '',
      method:"POST",
      header:{
        "cookie":wx.getStorageSync('sessionid'),
        "token":wx.getStorageSync('session'),
      }, 
     data:{
       phone:that.data.phone,
       search:that.data.search,
     },
     success (res) {
      console.log(res)
      that.data.status=res.data.status
    }
    })
  },
//减
reduce(e){
let i = e.currentTarget.dataset.index
let that = this
let food =  that.data.food
let newnumber = food[i].number  - 1
let newtotal = food[i].total - food[i].price
if(newnumber== 0){
  let newtotalprice =parseInt(that.data.total-food[i].price*100) 
food.splice(i,1)
this.setData({
  food:food,
  total:newtotalprice
})
}else{
let number = "food["+i+"].number"
let total = "food["+i+"].total"
let newtotalprice =parseInt(that.data.total-food[i].price*100) 
this.setData({
[number]:newnumber,
[total]:newtotal,
total:newtotalprice
})
// console.log(food[i].number)
}
// console.log(newnumber)
},
//输入框
number(e){
// console.log(e.detail.value)
let i = e.currentTarget.dataset.index
let that = this
let food =  that.data.food
let newnumber = e.detail.value
let newtotal = food[i].price *e.detail.value
// console.log(newtotal)
let number = "food["+i+"].number"
let total = "food["+i+"].total"
this.setData({
  [number]:newnumber,
  [total]:newtotal,
})
if (newnumber==0) {
  let newtotalprice =parseInt(that.data.total-food[i].total*100) 
  food.splice(i,1)
this.setData({
  food:food,
  total:newtotalprice
})
}else if(newnumber>food[i].number) {
  let newtotalprice =parseInt(that.data.total+food[i].price*100*(newnumber-food[i].number)) 
  // console.log(newtotalprice)
  this.setData({
  total:newtotalprice
  })
}else if (newnumber<food[i].number) {
  let newtotalprice =parseInt(that.data.total-food[i].price*100*(food[i].number-newnumber)) 
  // console.log(newtotalprice)
  this.setData({
    total:newtotalprice
    })
}
},
//加
plus(e){
  let i = e.currentTarget.dataset.index
  let that = this
  let food =  that.data.food
  let newnumber = parseInt(food[i].number)+parseInt(1)
  let newtotal = food[i].total + food[i].price
  let number = "food["+i+"].number"
  let total = "food["+i+"].total"
  let newtotalprice =parseInt(that.data.total+food[i].price*100)
  this.setData({
  [number]:newnumber,
  [total]:newtotal,
  total:newtotalprice
  })
},

onSubmit(){
let that = this.data.total
let a = parseInt(that/100)
console.log(a)
console.log("发起支付")
},

  showPopup() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });

  },
  shop:function(e){
console.log(e.currentTarget.id)
wx.navigateTo({
  url: '/pages/home/shop/shop?shopid='+[e.currentTarget.id],
})
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    let shoping = this.data.shoping;
    let shoptotal = this.data.shoptotal
    let total =  that.data.total
    // console.log( total)
    // console.log(shoptotal)
    let newtotal = parseInt( total+ that.data.shoptotal)
    // console.log(newtotal)
    this.setData({
        shoping:shoping,
        shoptotal:shoptotal
   })
   this.setData({
     food:that.data.food.concat(that.data.shoping),
     total:newtotal
  })
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onRefresh() {
    let that = this
    // console.log(666)
    if (this._freshing) return
    this._freshing = true
    setTimeout(() => {
      this.setData({
        triggered: false,
        shop:that.data.newshop
      })
      this._freshing = false
    }, 1000)
  },

  //上拉加载更多
  onReachBottom: function () {
    let that = this
    setTimeout(() => {
      this.setData({
        hidden: true,
        shop:that.data.shop.concat(that.data.newshop)
      })
   
    }, 1000)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})